<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../layui/css/layui.css" type="text/css">
    <script src="../layui/layui.js" type="text/javascript"></script>
    <script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
    <style>
        .layui-table-cell .layui-form-checkbox[lay-skin="primary"] {
            top: 50%;
            transform: translateY(-50%);
        }
        .layui-table-cell{
            text-align:center;
            height: auto!important;
            white-space: normal;
        }
    </style>
</head>
<body>

<div class="layui-container-fluid">
    <div class="layui-row">
        <div class="layui-col-xs12">
            <%--新增按钮--%>
            <button id="add_btn" class="layui-btn layui-btn-radius layui-btn-normal">新增上师</button>

            <div class="demoTable layui-input-inline">
                搜索：
                <div class="layui-inline">
                    <input class="layui-input" name="id" id="demoReload" autocomplete="off" placeholder="上师名字">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>

            <%--<button id="uploadExcel_btn" class="layui-btn layui-btn-radius layui-btn-normal">批量上传</button>--%>
            <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>批量上传</button>
            <button id="downExcel_btn" class="layui-btn layui-btn-radius layui-btn-normal">批量下载</button>
            <%--table表格--%>
            <table id="demo" lay-filter="demo"></table>
        </div>
    </div>
</div>


<%--新增表单--%>
<form class="layui-form" style="display: none;padding: 10px 30px 0px 0px;" id="addForm"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" name="guruName" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">法号</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" name="guruNickname" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上传头像</label>
        <input type="hidden" id="picPath" name="guruImage">
        <div class="layui-input-list">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>点击上传
            </button>
        </div>
    </div>
    <div>
        <label class="layui-form-label"></label>
        <img class="layui-upload-img" id="uploadImgId" width="80px" height="60px">
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否激活</label>
        <div class="layui-input-block">
            <input type="radio" name="guruStatus" value="1" title="是">
            <input type="radio" name="guruStatus" value="0" title="不是">
        </div>
    </div>
    <div class="layui-form-item"></div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addGuru">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script type="text/html" id="switchTpl">
    <input id="updateStatusId"  type="checkbox" name="guruStatus" value="{{d.guruId}}" lay-skin="switch" lay-text="激活|冻结" lay-filter="updateStatus" {{d.guruStatus ==0?'checked':''}}>
</script>

<script>
    //开启layui的各种组件
    layui.use(['layer','laydate','table','form','upload'],function () {
        var laydate=layui.laydate;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var upload=layui.upload;
        var path='${pageContext.request.contextPath}/';

        //一个修改弹出层，便于关闭弹出层
        var updateIndex;
        var addIndex;
        var addIndex1;

        //渲染table表格
        table.render({
            elem: '#demo'//id选择器选择表格
            ,url: '${pageContext.request.contextPath}/cmfzGuru/selectGuruByPage' //数据接口
            ,page: true //开启分页
            ,limit:3
            ,limits:[3,5,10]
            ,id: 'testReload'
            ,cols: [[ //表头
                {field: 'guruId', title: 'ID',sort:true}
                ,{field: 'guruName',title: '姓名'}
                ,{field: 'guruNickname',title: '法号'}
                ,{field: 'guruImage',title: '图片',templet: function(d){
                        return "<img src='"+path+d.guruImage+"' height='80'/>";
                    }}
                ,{field: 'guruStatus',title: '状态',templet: '#switchTpl', unresize: true}
                ,{title:"操作", width:300,align:'center',toolbar: '#barDemo'}//引入外部的toolbar进行渲染
            ]]
        });


        //监听冻结操作
        form.on('switch(updateStatus)', function(obj){
            //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            var guruId=obj.value;
            var status=this.checked ? '0' : '1';

            var index;
            $.ajax({
                url:"${pageContext.request.contextPath}/cmfzGuru/updateStatus",
                type:"post",
                beforeSend:function(){
                    index = layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
                },
                dataType:"json",
                data: {
                    "guruId":guruId,
                    "status":status
                },
                success:function (result) {
                    if (result.code==0){
                        //关闭弹出层，清空表单
                        layer.close(index);
                        table.reload('demo');
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        });

        /*
        搜索
        */
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key:demoReload.val()
                    }
                }, 'data');
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //指定允许上传的文件类型
        upload.render({
            elem: '#test3'
            ,url: '${pageContext.request.contextPath}/cmfzGuru/uploadExcel' //改成您自己的上传接口
            ,accept: 'file' //普通文件
            ,done: function(res){
                layer.msg(res.msg);
            }
        });


        //新增的弹出框
        $("#add_btn").click(function () {
            addIndex=layer.open({
                type: 1,
                content: $('#addForm'),
                area: ['500px', '500px']
            })
        });

        //头像的上传
        upload.render({
            elem: '#test1' //绑定元素
            ,url: '${pageContext.request.contextPath}/cmfzGuru/uploadGuruImg' //上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#uploadImgId').attr('src', result); //图片链接（base64）
                });
            },done: function(res){
                //上传完毕回调,给表单中赋值
                $("#picPath").val(res.path);
                layer.msg(res.msg);
            }
            ,error: function(res){
                //请求异常回调
                layer.msg(res.msg);
            }
        });


        //监听新增表单的提交 addGuru为按钮的lay-filter ,同时会把data数据带过来
        form.on("submit(addGuru)",function (data) {
            console.log(data.field);
            $.ajax({
                url:"${pageContext.request.contextPath}/cmfzGuru/addGuru",
                type:"post",
                dataType:"json",
                data: data.field,
                success:function (result) {
                    if (result.code==0){
                        //关闭弹出层，清空表单
                        layer.close(addIndex);
                        table.reload('demo');
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
            return false;//防止表单跳转
        });

        $("#downExcel_btn").click(function () {
            window.location.href = '${pageContext.request.contextPath}/cmfzGuru/downExcel';
        });


        //监听表格的几个按钮
        table.on("tool(demo)",function (obj) {
            var data=obj.data;// 拿到该行的数据
            var layEvent=obj.event; //事件
            //修改
            if (layEvent=='edit'){
                //弹出层，并且给表单赋值
                updateIndex=layer.open({
                    type: 1,
                    content: $('#updateForm')
                });
                //第一个为lay-filter
                form.val("updateForm",data);
            }

            if (layEvent=='del'){
                //删除操作
                layer.confirm('确定删除吗?', {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        url:"layui-user/deleteUser",
                        type:"post",
                        data:{id:data.id},
                        dataType:"json",
                        success:function (result) {
                            if (result.code==0){
                                //删除成功
                                layer.msg(result.msg);
                                //重新加载页面
                                table.reload('demo', {page: {
                                        curr: 1 //重新从第 1 页开始
                                    }});
                            } else {
                                layer.msg(result.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
    })
</script>

<%--toolbar用于引入--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>
</html>
